package com.example.finalexamcovid_19situations;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class maDaWei extends AppCompatActivity {
    Button button;
    RadioGroup radioGroup;
    RadioButton radioButton,radioButton2;
    String[][] data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ma_da_wei);

        button=(Button) findViewById(R.id.button);
        radioButton=(RadioButton) findViewById(R.id.radioButton);
        radioButton2=(RadioButton) findViewById(R.id.radioButton2);
        try {
            data= querySQL("china.db","total");
        }catch (RuntimeException runtimeException){
            Toast.makeText(this, "未发现数据，请联网后重试", Toast.LENGTH_SHORT).show();
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (radioButton.isChecked()){
                        Intent intent=new Intent(maDaWei.this,maDaWeiCnvas.class);
                        intent.putExtra("extra_date",data[0]);
                        intent.putExtra("extra_data",data[1]);
                        intent.putExtra("extra_title","感染总人数");
                        startActivity(intent);
                    }
                    if (radioButton2.isChecked()){
                        Intent intent=new Intent(maDaWei.this,maDaWeiCnvas.class);
                        intent.putExtra("extra_date",data[0]);
                        intent.putExtra("extra_data",data[2]);
                        intent.putExtra("extra_title","新增感染人数");
                        startActivity(intent);
                    }
                }catch (NullPointerException nullPointerException){
                    Toast.makeText(maDaWei.this, "未发现数据，请联网后重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //查询数据
    String[][] querySQL(String name,String tableName) {
        SQLiteDatabase sqLiteDatabase = openOrCreateDatabase(name, Context.MODE_PRIVATE,null);
        //查询获得游标
        Cursor cursor = sqLiteDatabase.query(tableName, null, null,
                null, null, null, null);
        String[][] data = new String[3][cursor.getCount()];
        //判断游标是否为空
        if (cursor.moveToFirst()) {
            //遍历游标，时间一个数组，总一个数组，增加一个数组
            for (int i = 0; i <cursor.getCount(); i++) {
                //通过移动到反方向，逆转数组
                cursor.moveToPosition(cursor.getCount()-1-i);
                //获得数据
                data[0][i]=cursor.getString(0);
                data[1][i]=cursor.getString(1);
                data[2][i]=cursor.getString(2);
//                data[i][0] = cursor.getString(0);
//                data[i][1] = cursor.getString(1);
//                data[i][2] = cursor.getString(2);
            }
        }
        return data;
    }
}